<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ################















if(Antz_IntelliForm::submitted('import-version')){
    $error = false;

    $packageid = (int) post('package');
    //$versionid = (int) post('version');

    if($packageid !== 0 && $DB->countRows($CONFIG->dbprefix.'vms_packages', "id = '{$packageid}'")==0){
        JFX::addError('Invalid package selected');
        $error = true;
    }/*else if($packageid !== 0){
        if($DB->countRows($CONFIG->dbprefix.'vms_versions', "id = '{$versionid}' AND package_id = '{$packageid}'")==0){
            JFX::addError('Invalid version');
            $error = true;
        }
    }*/

    if(!file_exists($_FILES['file']['tmp_name'])){
        $error = true;
        JFX::addError('No file attached, check max upload size in php.ini');
    }else{
        $fc = file_get_contents($_FILES['file']['tmp_name']);

        $import = unserialize($fc);
        if(!is_array($import)// ||
               //!array_key_exists('pData', $import) ||
               //!array_key_exists('vData', $import) ||
               //!array_key_exists('folders', $import) ||
               //!array_key_exists('files', $import) ||
               //!array_key_exists('langs', $import)
            ){
            JFX::addError('Invalid file upload');
            $error = true;
        }
    }

    if(!$error){

        if($packageid === 0){
            // auto-generate package
            if(false === $VM->importVersion($fc)){
                JFX::addError('Error importing');
            }else{
                JFX::addSuccess('Successfully imported');
            }

        }else{
            // append to existing package
            if(false === $VM->importVersion($fc, $packageid, $versionid)){
                JFX::addError('Error importing');
            }else{
                JFX::addSuccess('Successfully imported');
            }
        }

    }

}


$js = <<<JS
$(document).ready(function(){
/*
    $('#vms-import-package').change(function(){
        var packageid = this.options[this.selectedIndex].value;
        $.getJSON('{$this->adminUrl}/ajax/get-package-versions/', {'package' : packageid}, VMS_ImportPackage_ShowVersions);
    });*/
});

function VMS_ImportPackage_ShowVersions(json){
    var opts = Array();
    $('#vms-import-version option').each(function(){
        this.parentNode.removeChild(this);
    });
    for(var i=0; i<json.length; i++){
        var opt = new Option(json[i].version, json[i].id);
        $('#vms-import-version').append(opt);
    }
}
JS;


$VIEW->addJs($js);



$versionOpts = array();
$packageOpts = array();

$packages = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_packages ORDER BY keyname ASC");
$packageOpts[0] = 'New package...';
foreach($packages as $k=>$v){
    $packageOpts[$v['id']] = $this->getContentLang('package_title', $v['id']);
}

$packageid = (int) post('package', 0);
if($DB->countRows($CONFIG->dbprefix.'vms_packages', "id = '{$packageid}'")>0){
    $versions = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_versions WHERE package_id = '{$packageid}' ORDER BY version DESC");
    foreach($versions as $k=>$v){
        $versionOpts[$v['id']] = $v['version'];
    }
}

$formFields = array(
    array(
        'type' => 'file',
        'name' => 'file',
        'label' => 'File',
        'value' => ''
    ),
    array(
        'type' => 'select',
        'name' => 'package',
        'id' => 'vms-import-package',
        'label' => 'Package',
        'value' => post('package'),
        'options' => $packageOpts
    ),/*
    array(
        'type' => 'select',
        'name' => 'version',
        'id' => 'vms-import-version',
        'value' => post('version'),
        'label' => 'Version',
        'options' => $versionOpts
    ),*/
    array(
        'type' => 'submit',
        'value' => 'Import',
        'label' => '&nbsp;',
        'name' => 'submitBtn'
    )
);

echo JFX::makeRapidForm($formFields, 'Import Details', 'import-version');







